home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48hor2 / filter.src < prev    next >
Text File  |  1991-07-25  |  5KB  |  140 lines

  1. %%HP: T(3)A(R)F(.);
  2. DIR
  3.  
  4.   DEMO                 @ Generate the data and call the filter demo.
  5.     \<< RAD            @ RAD will set radians mode.  You may want to
  6.                        @ remove it if you change Bpar's function.
  7. Bpar BUILD             @ Get BUILD parameters.  Call BUILD.
  8. PLOT 2 WAIT TEXT 40    @ Plot the good data.  Pause a bit.
  9. NOISE PLOT 2 WAIT      @ Add noise of amplitude 40 Peak to Peak.
  10.                        @ Plot the noisy data and pause again.
  11.                        @ You may want to change this noise value
  12.                        @ if you change the function in Bpar.
  13. FDMO                   @ Call the filter demo.
  14.     \>>
  15.  
  16. @ Filter Demo.
  17. @ Assumes the summation array is valid and contains the data
  18. @ you want filtered.  Loops 5 times, redisplaying the data
  19. @ after each loop.
  20.  
  21.   FDMO
  22.     \<< 1 5            @ Call the filter algorith 5 times.
  23.       FOR i TEXT
  24. CLLCD
  25. "Filtering data...
  26. Loop "
  27. i + 1 DISP \GSDAT      @ Recall sum data to stack.
  28. SMOOTH '\GSDAT' STO    @ Filter it. Store it.
  29. PLOT 2 WAIT            @ Plot it.
  30.       NEXT 7 FREEZE    @ Freeze display if last pass.
  31.     \>>
  32.  
  33. @ Bpar
  34. @ This list defines the function that will generate the initial
  35. @ "clean" data.  The first element of the list is a function that
  36. @ expects the X value on the stack.  After evaluating the function,
  37. @ the Y value is on the stack.  The 2nd element is the starting X
  38. @ value.  The 3rd is the ending X value.
  39.  
  40.   Bpar {
  41.     \<< SIN 100 *      @ The function used by the demo is '100 * SIN(X)'
  42.     \>> 0 12.5 }       @ The starting X value and ending X value.
  43.  
  44. @ BUILD
  45. @ Builds summation data array based on Bpar values.
  46. @ Expects a list (just like Bpar) on stack level 1.
  47.  
  48.   BUILD
  49.     \<< OBJ\-> DROP DUP @ Get the individual elements from the list.
  50. 131 / RCLF \-> f        @ Take 131 samples.  Save flags.
  51. start end step
  52. flags
  53.       \<< CLLCD
  54. "Building \GSDAT.
  55. Patience please...
  56.  ...I aint a Cray!"
  57. 1 DISP 1 FIX CL\GS      @ Display the prompt.  Clear sum data.
  58. start end
  59.         FOR i end i     @ Display our progress.
  60. %T "%" + 4 DISP i       @ Get the current X value.
  61. DUP f EVAL \->V2 \GS+   @ Calculate Y form a vector.  Sum it away.
  62. step                    @ Do next X.
  63.         STEP flags
  64. STOF
  65.       \>>
  66.     \>>
  67.  
  68. @ PLOT
  69. @ Assumes the summation array exists.
  70. @ Will draw a scatter plot based on the X values
  71. @ in column 1 and the Y values in column 2.
  72.  
  73.   PLOT                  @ SCATTER PLOT of summed data
  74.     \<< 1 XCOL 2 YCOL
  75. SCATRPLOT
  76.     \>>
  77.  
  78. @ SMOOTH is the main filtering (or smoothing) algorithm.
  79. @ SMOOTH takes all y values (column 2) in the array and
  80. @ averages them with the adjacent y values.
  81. @ SMOOTH expects the summation data array on the stack.
  82. @ It returns the filtered array to the stack when finished.
  83. @ All you DSP experts out there can hack away at this or provide
  84. @ your own algorithm.  As long as it's inputs and outputs are the
  85. @ same, it should work well with the rest of these routines.
  86.  
  87.   SMOOTH                @ The main filtering algorithm.
  88.     \<< OBJ\-> DUP 1    @ Break up array and determine size.
  89. GET 2 * 3 PICK \->      @ Get the number of stack elements to roll.
  90. dim ec y                @ Save initial y endpoint
  91.       \<< 1 ec 2 / 1    @ Average all y values in the array.
  92. -
  93.         START 3         @ Get adjacent y values and average them.
  94. PICK + 5 PICK + 3 /
  95. y SWAP 'y' STO ec       @ Save current average and restore the last one.
  96. ROLLD ec ROLLD          @ Roll twice to get the next xy pair.
  97.         NEXT ec
  98. ROLLD ec ROLLD dim      @ Reassemble the array.
  99. \->ARRY
  100.       \>>
  101.     \>>
  102.  
  103. @ NOISE adds random data to all y values in the
  104. @ array.  The peak to peak noise amplitude is on
  105. @ level 1.  It assumes a valid array is stored in
  106. @ the summation data array variable.
  107.  
  108.   NOISE                 @ Add noise to all y data values.
  109.     \<< CLLCD
  110. "Adding noise..." 1
  111. DISP \GSDAT OBJ\-> DUP
  112. 1 GET 2 * DUP 3 +
  113. ROLL \-> dim ec noise
  114.       \<< 1 ec 2 /
  115.         START RAND
  116. .5 - noise * + ec
  117. ROLLD ec ROLLD
  118.         NEXT dim
  119. \->ARRY '\GSDAT' STO
  120.       \>>
  121.     \>>
  122.  
  123. @ LOOP calls the SMOOTH routine n times.
  124. @ The array is on level 2 and n is on level 1.
  125. @ LOOP is not used by the demo.  It is usefull
  126. @ for repeating the SMOOTH algorithm without
  127. @ the delays caused by replotting the data as
  128. @ the demo does.
  129.  
  130.   LOOP
  131.     \<< 1 SWAP
  132.       FOR i CLLCD
  133. "Filtering data...
  134. Loop "
  135. i + 1 DISP SMOOTH
  136.       NEXT
  137.     \>>
  138.  
  139. END
  140.